﻿using System;
using System.Collections.Generic;

using TradeClient.Logic.Entity;
using WAF.EntityFramework;

namespace TradeClient.Logic.TradeCore
{
    /// <summary>
    /// 持仓管理类
    /// </summary>
    public class PositionManager
    {
        /// <summary>
        /// 单例模式实例对象
        /// </summary>
        private static PositionManager instance;


        /******************************** Public ********************************/

        public Dictionary<string, Position> Positions = new Dictionary<string, Position>(100);

        /// <summary>
        /// 取得持仓管理类实例
        /// </summary>
        /// <returns>交易管理类实例</returns>
        public static PositionManager GetInstance()
        {
            if (instance == null)
                instance = new PositionManager();

            return instance;
        }

        /// <summary>
        /// 通过Id获取指定持仓信息
        /// </summary>
        /// <param name="id">持仓Id</param>
        /// <returns>持仓类</returns>
        public Position GetPositionById(string id)
        {
            return this.Positions.ContainsKey(id) ? this.Positions[id] : null;
        }

        /// <summary>
        /// 获取要平仓的持仓对象
        /// </summary>
        /// <param name="tradeField">开仓字段信息</param>
        /// <returns>持仓类</returns>
        public Position GetClosePosition(TradeField tradeField)
        {
            TradeField field = new TradeField();
            field.Policy = tradeField.Policy;
            field.Market = tradeField.Market;
            field.Account = tradeField.Account;
            field.Code = tradeField.Code;
            field.Level = tradeField.Level;
            field.Unit = tradeField.Unit;

            //使用反方向查找持仓
            field.Direction = tradeField.Direction == DirectionType.Buy ? DirectionType.Sell : DirectionType.Buy;

            return this.Positions.ContainsKey(field.GetId()) ? this.Positions[field.GetId()] : null;
        }

        /// <summary>
        /// 增加持仓
        /// </summary>
        /// <param name="tradeField">开仓字段信息</param>
        public Position Create(TradeField tradeField)
        {
            Position position = new Position(tradeField);

            this.Positions[position.Id] = position;

            return position;
        }

        /// <summary>
        /// 移除持仓
        /// </summary>
        /// <param name="position">持仓类</param>
        public void Remove(Position position)
        {
            this.Positions.Remove(position.Id);
        }


        /******************************** private ********************************/

        /// <summary>
        /// 私有化构造函数（读取数据库中的持仓数据）
        /// </summary>
        private PositionManager()
        {
            PositionRecord[] positionRecords = EntityAction.Select<PositionRecord>();

            foreach (PositionRecord positionRecord in positionRecords)
            {
                Position position = new Position(positionRecord);
                this.Positions[position.Id] = position;
            }
        }
    }
}
